package com.bocloud.cmp.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.bocloud.cmp.dao.NetworkCardDao;
import com.bocloud.cmp.entity.resource.NetworkCard;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.JdbcGenericDao;
import com.bocloud.database.utils.SQLHelper;

@Service("networkCardDao")
public class NetworkCardDaoImpl extends JdbcGenericDao<NetworkCard, Long> implements NetworkCardDao {

	@Override
	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.id,a.name from network_card a where a.is_deleted = 0";
		sql = SQLHelper.buildSql(sql, 1, Integer.MAX_VALUE, params, sorter, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		List<NetworkCard> NetworkCards = this.list(NetworkCard.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<>();
		for (NetworkCard NetworkCard : NetworkCards) {
			beans.add(new SimpleBean(NetworkCard.getId(), NetworkCard.getName()));
		}
		return beans;
	}

	@Override
	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from network_card a where a.is_deleted = 0";
		sql = SQLHelper.buildRawSql(sql, params, null, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	@Override
	public boolean removeByVid(Long vendorId, Long userId) throws Exception {
		String sql = "update network_card set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	@Override
	public List<NetworkCard> listByVendor(Long vendorId) throws Exception {
		String sql = "select a.* from network_card a where a.is_deleted = 0 and a.vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		return this.list(NetworkCard.class, sql, params);
	}

	@Override
	public Boolean delete(Long id, Long userId) throws Exception {
		String sql = "update network_card set is_deleted = 1 where id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	@Override
	public NetworkCard query(Long id) throws Exception {
		String sql = "select a.*,b.name vendor_name from network_card a left join cloud_vendor b on a.vendor_id = b.id where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<NetworkCard> list = this.list(NetworkCard.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public List<NetworkCard> listByHost(Long hostId, String category) throws Exception {
		String sql = "select a.* from network_card a where a.is_deleted = 0 and a.host_id = :hostId and category = :category";
		Map<String, Object> params = MapTools.simpleMap("hostId", hostId);
		params.put("category", category);
		return this.list(NetworkCard.class, sql, params);
	}

	@Override
	public boolean deleteByHost(Long hostId, String category, Long userId) throws Exception {
		String sql = "delete from network_card where host_id = :hostId and category = :category";
		Map<String, Object> params = MapTools.simpleMap("hostId", hostId);
		params.put("category", category);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

}
